//
//  ZJMessageFrame.m
//  QQ_Chat_UI
//
//  Created by ZJ on 2018/7/31.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ZJMessageFrame.h"
#import "ZJMessage.h"
#import <UIKit/UIKit.h>
#import "NSString+ZJAddition.h"

const CGFloat ZJMessageFrameDefaultFontSize = 13.0;

@implementation ZJMessageFrame

- (void)setMessage:(ZJMessage *)message {
    _message = message;
    
    // 计算每个控件的 frame
    CGFloat screenW = CGRectGetWidth(UIScreen.mainScreen.bounds);
    
    CGFloat margin = 5;
    
    // 计算time的frame
    CGFloat timeX = 0;
    CGFloat timeY = 0;
    CGFloat timeW = screenW;
    CGFloat timeH = 15;
    
    if (!message.hideTime) {
        _timeFrame = CGRectMake(timeX, timeY, timeW, timeH);
    }
    
    // 计算头像的frame
    CGFloat iconW = 30;
    CGFloat iconH = iconW;
    CGFloat iconY = CGRectGetMaxY(_timeFrame) + margin;
    CGFloat iconX = message.type == ZJMessageTypeOther ? margin : (screenW - iconW - margin);
    _iconFrame = CGRectMake(iconX, iconY, iconW, iconH);
    
    NSDictionary *attr = @{
                           NSFontAttributeName : [UIFont systemFontOfSize:ZJMessageFrameDefaultFontSize],
                           };
    // 计算消息正文的frame
    CGSize textSize = [message.text sizeOfTextWithMaxSize:CGSizeMake(200, MAXFLOAT) attributes:attr];
    CGFloat textW = textSize.width + 40;
    CGFloat textH = textSize.height + 30;
    CGFloat textY = iconY;
    CGFloat textX = message.type == ZJMessageTypeOther ? CGRectGetMaxX(_iconFrame) : (screenW - textW - iconW - margin);
    _textFrame = CGRectMake(textX, textY, textW, textH);
    
    // 计算行高
    CGFloat maxY = MAX(CGRectGetMaxY(_textFrame), CGRectGetMaxY(_iconFrame));
    _rowHeight = maxY;
}

@end
